package com.cskaoyan.controller.backStage;

import com.cskaoyan.exception.MobileValidationException;
import com.cskaoyan.exception.NickNameValidationException;
import com.cskaoyan.model.User;
import com.cskaoyan.model.vo.BaseRespVO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.service.UserService;
import com.cskaoyan.util.ValidationUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @description：完成会员管理功能
 * @author     ：huzhouming
 * @date       ：2022/5/5 17:08
 */
@RestController
@RequestMapping("admin/user")
public class UserController {

    @Autowired
    UserService userService;
    @RequiresPermissions("admin:user:list")
    @RequestMapping("list")
    /**
     * @author: huzhouming
     * @description: 获取用户管理模块会员管理信息
     * @date: 2022/5/5
     * @param username
     * @param param
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public BaseRespVO userList(String username, BaseParamBO param,String mobile) {
        DataVO userDataVO = userService.query(param,username,mobile);
        return BaseRespVO.ok(userDataVO);
    }
    @RequiresPermissions("admin:user:update")
    @PostMapping("update")
    /**
     * @author: huzhouming
     * @description: 修改用户信息
     * @date: 2022/5/5
     * @param user
     * @return com.cskaoyan.model.vo.BaseRespVO
     */
    public BaseRespVO update(@RequestBody@Validated User user, BindingResult bindingResult) throws NickNameValidationException, MobileValidationException {
        ValidationUtil.validMobile(bindingResult);
        try {
            userService.updateUser(user);
        }catch (Exception e){
            throw new NickNameValidationException();
        }
        return BaseRespVO.ok(1);
    }
    @RequiresPermissions("admin:user:detail")
    @GetMapping("detail")
    public BaseRespVO detail(String id){
        User user;
        try {
            int i = Integer.parseInt(id);
            user = userService.selectUserById(i);
        }catch (Exception e){
            return BaseRespVO.parameterError("参数值不对");
        }
        return BaseRespVO.ok(user);
    }
}
